import axios from 'axios';
import qs from 'query-string';

export interface CardId {
    id: number;
}

export interface CardRecord {
    id: number;
    company_id: number;
    name: string;
    image: string;
    image_url: string;
    sort_by: number;
    score: number;
    expired_days: number;
    created_at: string;
    updated_at: string;
}

export interface CardListRes {
    list: CardRecord[];
    total: number;
}

export interface CardModel {
    id: number;
    company_id: number;
    name: string;
    image: string;
    sort_by: number;
    score: number;
    status: number;
    expired_days: number;
}

export interface CardParams {
    current: number;
    pageSize: number;
    company_id?: number;
    created_at?: string;
}

export interface BatchGenerateCardItemParams {
    card_id: number;
    count: number;
    expired_at?: number;
    score?: number;
}

export interface BatchGenerateCardItemResponse {
    count: number;
    cards: Array<{
        id: number;
        card_number: string;
        password: string;
    }>;
}

export interface CardBindRecord {
    id: number;
    user_id: number;
    card_id: number;
    card_item_id: number;
    expired_at: string;
    status: number;
    score: number;
    created_at: string;
    updated_at: string;
    user_name: string;
    user_phone: string;
    card_name: string;
    card_number: string;
    password: string;
    status_text: string;
}

export interface CardBindRecordListRes {
    list: CardBindRecord[];
    total: number;
}

export interface CardBindRecordParams {
    current: number;
    pageSize: number;
    search?: string;
}

/**
 * 获取福利卡列表
 * @param params
 */
export function queryCardList(params: CardParams) {
    return axios.get<CardListRes>('/admin/card/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取福利卡详情
 * @param params
 */
export function queryCardOne(params: CardId) {
    return axios.get<CardRecord>('/admin/card/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 提交福利卡表单（创建/更新）
 * @param data
 */
export function submitCardForm(data: CardModel) {
    return axios.post('/admin/card/submit', data);
}

/**
 * 删除福利卡
 * @param id
 */
export function delCard(id: number) {
    return axios.post('/admin/card/delete', { id });
}

/**
 * 批量生成卡数据
 * @param data
 */
export function batchGenerateCardItems(data: BatchGenerateCardItemParams) {
    return axios.post<{ data: BatchGenerateCardItemResponse }>('/admin/cardItem/batchGenerate', data);
}

/**
 * 获取富利卡绑定记录列表
 * @param params
 */
export function queryCardBindRecords(params: CardBindRecordParams) {
    return axios.get<CardBindRecordListRes>('/admin/card/bindRecords', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}
